//
//  VideoPlayerControlView.m
//  VideoPlayer
//
//  Created by HN on 2021/6/30.
//

#import "VideoPlayerControlView.h"

static NSInteger padding = 8;

@interface VideoPlayerControlView ()
//当前时间
@property(nonatomic, strong) UILabel *timeLabel;
//总时间
@property(nonatomic, strong) UILabel *totalTimeLabel;
//进度条
@property(nonatomic, strong) UISlider *slider;
//缓存进度条
@property(nonatomic, strong) UISlider *bufferSlier;
@end


@implementation VideoPlayerControlView

#pragma mark - life
- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

- (void)dealloc {
    NSLog(@"%s",__func__);
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - UI
- (void)setupUI {
    [self addSubview:self.timeLabel];
    [self addSubview:self.bufferSlier];
    [self addSubview:self.slider];
    [self addSubview:self.totalTimeLabel];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat heightTemp = 30;
    CGFloat widthTemp = 50;
    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
    self.timeLabel.frame = CGRectMake(0, (height - heightTemp) / 2., widthTemp, heightTemp);
    
    self.totalTimeLabel.frame = CGRectMake(width - widthTemp - padding, (height - heightTemp) / 2., widthTemp, heightTemp);
    self.slider.frame = CGRectMake(widthTemp + padding, 0, width - (widthTemp * 2 + padding * 3), height);
    self.bufferSlier.frame = CGRectMake(widthTemp + padding, 0, width - (widthTemp * 2 + padding * 3), height);
}

#pragma mark - other
- (void)handleSliderPosition:(UISlider *)slider {
    self.bTouchingSlider = YES;
}

- (void)onActionTouchUpOutInside:(UISlider *)sender {
    self.bTouchingSlider = NO;
    if ([self.delegate respondsToSelector:@selector(controlView:draggedPositionWithSlider:)]) {
        [self.delegate controlView:self draggedPositionWithSlider:self.slider];
    }
}

- (void)handleTap:(UITapGestureRecognizer *)gesture {
    CGPoint point = [gesture locationInView:self.slider];
    CGFloat pointX = point.x;
    CGFloat sliderWidth = self.slider.frame.size.width;
    CGFloat currentValue = pointX / sliderWidth * self.slider.maximumValue;
    if ([self.delegate respondsToSelector:@selector
         (controlView:pointSliderLocationWithCurrentValue:)]) {
        [self.delegate controlView:self pointSliderLocationWithCurrentValue:currentValue];
    }
}

#pragma mark - get data
//懒加载
- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = [UIColor whiteColor];
    }
    return _timeLabel;
}

- (UILabel *)totalTimeLabel {
    if (!_totalTimeLabel) {
        _totalTimeLabel = [[UILabel alloc] init];
        _totalTimeLabel.textAlignment = NSTextAlignmentLeft;
        _totalTimeLabel.font = [UIFont systemFontOfSize:12];
        _totalTimeLabel.textColor = [UIColor whiteColor];
    }
    return _totalTimeLabel;
}

- (UISlider *)slider {
    if (!_slider) {
        _slider = [[UISlider alloc] init];
        _slider.continuous = YES;
        self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        [_slider addTarget:self action:@selector(handleSliderPosition:) forControlEvents:UIControlEventValueChanged];
        [_slider addTarget:self action:@selector(onActionTouchUpOutInside:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
        [_slider addGestureRecognizer:self.tapGesture];
        _slider.maximumTrackTintColor = [UIColor clearColor];
        _slider.minimumTrackTintColor = [UIColor whiteColor];
    }
    return _slider;
}

- (UISlider *)bufferSlier {
    if (!_bufferSlier) {
        _bufferSlier = [[UISlider alloc] init];
        [_bufferSlier setThumbImage:[UIImage new] forState:UIControlStateNormal];
        _bufferSlier.continuous = YES;
        _bufferSlier.minimumTrackTintColor = [UIColor redColor];
        _bufferSlier.minimumValue = 0.f;
        _bufferSlier.maximumValue = 1.f;
        _bufferSlier.userInteractionEnabled = NO;
    }
    return _bufferSlier;
}

#pragma mark - set data
- (void)setValue:(CGFloat)value {
    self.slider.value = value;
}

- (CGFloat)value {
    return self.slider.value;
}

- (void)setMinValue:(CGFloat)minValue {
    self.slider.minimumValue = minValue;
}

- (CGFloat)minValue {
    return self.slider.minimumValue;
}

- (void)setMaxValue:(CGFloat)maxValue {
    self.slider.maximumValue = maxValue;
}

- (CGFloat)maxValue {
    return self.slider.maximumValue;
}

- (void)setCurrentTime:(NSString *)currentTime {
    self.timeLabel.text = currentTime;
}

- (NSString *)currentTime {
    return self.timeLabel.text;
}

- (void)setTotalTime:(NSString *)totalTime {
    self.totalTimeLabel.text = totalTime;
}

- (NSString *)totalTime {
    return self.totalTimeLabel.text;
}

- (CGFloat)bufferValue {
    return self.bufferSlier.value;
}

- (void)setBufferValue:(CGFloat)bufferValue {
    self.bufferSlier.value = bufferValue;
}

@end
